package com.yfp.tissue.service.impl;

import cn.hutool.core.lang.Assert;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.uuid.Snowflake;
import com.yfp.common.datascope.annotation.DataScope;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.system.api.model.LoginUser;
import com.yfp.tissue.domain.Dto.RouteDTO;
import com.yfp.tissue.domain.entity.Area;
import com.yfp.tissue.domain.entity.Route;
import com.yfp.tissue.domain.vo.PositionVO;
import com.yfp.tissue.domain.vo.RouteVO;
import com.yfp.tissue.mapper.*;
import com.yfp.tissue.mapper.*;
import com.yfp.tissue.service.AreaService;
import com.yfp.tissue.service.RouteService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @PROJECT_NAME: ydyfp
 * @DESCRIPTION:
 * @USER: xiaohuya
 * @DATE: 2023/12/16 11:27
 */

@Service
@Slf4j

public class RouteServiceImpl implements RouteService {
    @Resource
    private RouteMapper routeMapper;
    @Resource
    private PositionMapper positionMapper;
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private SysUserCompanyMapper sysUserCompanyMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private AreaService areaService;

    @DataScope(deptAlias = "rt")
    @Override
    public List<Route> queryRoute(RouteDTO routeDTO) {
        List<Route> routes = routeMapper.queryAllByLimit(routeDTO);
        return routes;
    }

    @Override
    public List<Route> findList(RouteDTO routeDTO) {
        return routeMapper.findList(routeDTO);
    }

    @Override
    public R routeAdd(Route route) {
        Snowflake snowflake = new Snowflake(1);
        Assert.notBlank(route.getAreaId(), "区域id不能为空");

        Area area = areaService.getById(route.getAreaId());
        if (route.getDescribe() != null && route.getDescribe().length() > 300) {
            return R.fail("线路描述大于300");

        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long deptId = loginUser.getSysUser().getDeptId();

        route.setUpdateName(loginUser.getSysUser().getNickName());
        route.setEnable("1");
        route.setRouteId(String.valueOf(snowflake.nextId()));
        route.setDeptId(deptId);
        route.setCityName(area.getCityName());
        routeMapper.insert(route);
        return R.ok();
    }

    @Override
    public R updateRoute(Route route) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        log.info("入参{}", route);

        List<Route> routes = routeMapper.queryArea(route);
        log.info("查询结果{}", routes);
        List<Route> routeList = routes.stream().filter(e -> e.getName().equals(route.getName()) && !e.getRouteId().equals(route.getRouteId())).toList();
        log.info("筛选结果{}", routeList);

        route.setUpdateName(loginUser.getSysUser().getNickName());
        Area area = areaService.getById(route.getAreaId());
        route.setCityName(area.getCityName());
        routeMapper.update(route);
        return R.ok();
    }

    @Override
    public R routeDetail(String routeId) {
        RouteVO routeVO = routeMapper.routeDetail(routeId);
        RouteDTO routeDTO = new RouteDTO();
        routeDTO.setRouteId(routeVO.getRouteId());
        List<PositionVO> areaVOS = positionMapper.queryPosition(routeDTO);
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("routeVO", routeVO);
        objectObjectHashMap.put("positions", areaVOS);
        return R.ok(objectObjectHashMap);
    }

    @Override
    public Route getById(String routeId) {
        return routeMapper.getById(routeId);
    }


}
